package com.flx.multi.thread.juc.semaphore;

import java.util.concurrent.Semaphore;
import java.util.stream.IntStream;

/**
 * @Author Fenglixiong
 * @Create 2021/11/17 15:31
 * @Description 多票据使用逻辑
 **/
public class MultiSemaphore {

    public static void main(String[] args) {

        Semaphore semaphore = new Semaphore(3);

        IntStream.rangeClosed(1,10).forEach(x->{
            new Thread(()->{
                try {
                    System.out.println("T"+x+" 线程启动了...");
                    semaphore.acquire();
                    System.out.println("T"+x+" 获取到票据，开始执行...");
                    Thread.sleep(22000);
                    System.out.println("T"+x+" 执行完毕，释放票据...");
                    semaphore.release(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        });

    }

}
